package com.crayfish.quduo.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.widget.Toolbar;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.crayfish.quduo.App;
import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseActivity;
import com.crayfish.quduo.commom.Params;
import com.crayfish.quduo.utils.DataCleanManager;
import com.crayfish.quduo.utils.NoticeUtil;
import com.crayfish.quduo.utils.SharedPreferencesUtil;
import com.crayfish.quduo.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.BmobUpdateListener;
import cn.bmob.v3.update.BmobUpdateAgent;
import cn.bmob.v3.update.UpdateResponse;
import cn.bmob.v3.update.UpdateStatus;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;

/**
 * Created by crayfish on 2017/1/23.
 */
public class SettingActivity extends BaseActivity{

    private SettingActivity instance = SettingActivity.this;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.news_switch)
    Switch news_switch;
    @BindView(R.id.ll_cache)
    LinearLayout ll_cache;
    @BindView(R.id.txt_cache)
    TextView txt_cache;
    @BindView(R.id.txt_about)
    TextView txt_about;
    @BindView(R.id.txt_exit)
    TextView txt_exit;
    @BindView(R.id.txt_update)
    TextView txt_update;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initEventAndData() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        txt_cache.setText(DataCleanManager.getTotalCacheSize(this));
        news_switch.setChecked((Boolean) SharedPreferencesUtil.getData(App.getInstance().getApplicationContext(), Params.IS_NEW,false));
        news_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                NoticeUtil.showNotice(instance,"测试消息","谢谢使用");
                SharedPreferencesUtil.saveData(App.getInstance().getApplicationContext(), Params.IS_NEW,isChecked);
            }
        });
    }

    @OnClick(R.id.ll_cache)
    void cacheClick(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.warn_dialog)
                .setMessage(R.string.is_clear_cache)
                .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DataCleanManager.clearAllCache(instance);
                        txt_cache.setText(DataCleanManager.getTotalCacheSize(instance));
                    }
                })
                .setNegativeButton(R.string.no,null)
                .create();
        builder.show();
    }

    @OnClick(R.id.txt_update)
    void updateClick(){
        BmobUpdateAgent.forceUpdate(this);
    }

    @OnClick(R.id.txt_about)
    void aboutClick(){
        startMyActivity(AboutActivity.class);
    }

    @OnClick(R.id.txt_exit)
    void exitClick(){
        if(null == SharedPreferencesUtil.getUser(this)){
            ToastUtil.show("请先登录帐号");
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.warn_dialog)
                .setMessage(R.string.is_exit_account)
                .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        clearAccount();
                        finish();
//                        App.getInstance().exitApp();
                    }
                })
                .setNegativeButton(R.string.no,null)
                .create();
        builder.show();
    }

    /**
     * 清除授权信息
     */
    private void clearAccount(){
        SharedPreferencesUtil.clearUser(this);
        BmobUser.logOut();
        ShareSDK.initSDK(this);
        Platform[] plats = ShareSDK.getPlatformList();
        for (Platform platform : plats){
            platform.removeAccount();
        }
    }
}
